/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName FacebookStageWebView.as
 * @creationDate Aug 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.socialize.facebook
{
	import com.gesturesoft.imultimax.socialize.org.osflash.signals.Signal;
	import com.gesturesoft.imultimax.views.components.BusyPopUp;
	
	import flash.display.Stage;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.LocationChangeEvent;
	import flash.events.SoftKeyboardEvent;
	import flash.events.StageOrientationEvent;
	import flash.geom.Orientation3D;
	import flash.geom.Rectangle;
	import flash.media.StageWebView;
	
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	
	public class FacebookStageWebView extends UIComponent
	{
		private var stageWebView:StageWebView;
		private var myStage:Stage;
		private var __url:String;
		private var __text:String;
		private var __yOffset:int = 0;
		private var __heightOffset:int = 0;
		private var __facebookService:FacebookService;
		
		private var __busyPopUp:BusyPopUp;
		
		public var locationChangeSignal:Signal = new Signal();
		public var locationChangingSignal:Signal = new Signal();
		public var completeSignal:Signal = new Signal();
		
		
		public function FacebookStageWebView()
		{
			
			addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			
		}
		
		public function hide():void
		{
			
			stageWebView.stage = null;
			
		}
		
		public function show():void
		{
			
			stageWebView.stage = myStage;
			
		}
		
		public function dispose():void
		{
			
			trace("dispose");
			hide();
			stageWebView.dispose();
			
		}
		
		protected function addedToStageHandler(event:Event):void
		{
			
			myStage = stage;
			removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
			myStage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE,onOrientationChange);
			measureBox();
			hide();
			
		}
		
		protected function measureBox():void
		{

			stageWebView = new StageWebView();
			stageWebView.stage = myStage;
			stageWebView.viewPort = new Rectangle(0, yOffset, myStage.fullScreenWidth, myStage.fullScreenHeight - heightOffset);
			stageWebView.addEventListener(ErrorEvent.ERROR, errorHandler);
			stageWebView.addEventListener(Event.COMPLETE, onComplete);
			stageWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGE, locationChangeHandler);
			stageWebView.addEventListener(LocationChangeEvent.LOCATION_CHANGING, locationChangingHandler);
			
			if (__url) 
			{
				
				stageWebView.loadURL(__url);
				
			}
			else if (__text) 
			{
				
				stageWebView.loadString(__text);
				
			}
			
		}
		
		protected function onComplete(e:Event):void
		{
			
			completeSignal.dispatch(e.clone());
			
		}
		
		protected function onOrientationChange(event:StageOrientationEvent):void
		{
			if(stage)
			{
				
				callLater(resize);
				
			}
			
		}
		
		protected function resize():void
		{
			
			stageWebView.viewPort = new Rectangle(0, yOffset, myStage.fullScreenWidth, myStage.fullScreenHeight - heightOffset);
			
		}
		
		protected function locationChangingHandler(event:LocationChangeEvent):void
		{
			
			locationChangingSignal.dispatch(event.clone());
			
		}
		
		protected function locationChangeHandler(event:LocationChangeEvent):void
		{
			
			if (event.location.indexOf('?code=')>0) 
			{
				
				var locAr:Array = event.location.split("?code=");
				this.facebookService.fetchAccessToken(String(locAr[1]));
				
			}
			
			locationChangeSignal.dispatch(event.clone());
			
		}
		
		protected function errorHandler(event:Event):void
		{
			
			dispatchEvent(event.clone());
			
		}

		protected function serviceErrorHandler(errMessage:String):void
		{
			
			trace("Error received by FacebookStageWebView: "+errMessage);
			
		}
		
		protected function userInfoReceivedHandler(userData:Array):void
		{
			
			trace("userInfoReceivedHandler");
			
		}
		
		public function get yOffset():Number
		{
			
			return __yOffset;
			
		}
		
		public function set yOffset(val:Number):void
		{
			
			__yOffset = val;
			
		}
		
		public function get heightOffset():Number
		{
			
			return __heightOffset;
			
		}
		
		public function set heightOffset(val:Number):void
		{
			
			__heightOffset = val;
			
		}
		
		public function get facebookService():FacebookService
		{
			
			return __facebookService;
			
		}
		
		public function set facebookService(val:FacebookService):void
		{
			
			__facebookService = val;
			__facebookService.userInfoReceived.addOnce(userInfoReceivedHandler);
			
		}
		
		public function set url(url:String):void
		{
			
			__url = url;
			
			if(stageWebView)
			{
				
				stageWebView.loadURL(url);
				
			}
			
		}
		
		public function set text(text:String):void
		{
			
			__text = text;
			
			if(stageWebView)
			{
				
				stageWebView.loadString(text);
				
			}
			
		}
		
	}
	
}